// GView.h: interface for the GView class.
//
//////////////////////////////////////////////////////////////////////

#ifndef __GVIEW_H__
#define __GVIEW_H__

#include "GForm.h"

class GView : public GForm  
{
public:
	GView(GForm* parent = 0);
	virtual ~GView();

	enum ScollBar { HSCROLL = 0, VSCROLL };

	void              setViewScroll(ScollBar type, int pos = 0, int max = 100);
    void              getViewScroll(ScollBar type, int* pos , int* range ) const 
    { 
        switch (type){
        case VSCROLL: *pos = m_vCurPos; *range = m_vRange; break;
        case HSCROLL: *pos = m_hCurPos; *range = m_hRange; break;
        }
    }
	int               getVertPos()const { return m_vCurPos;}
	int               getHorzPos()const { return m_hCurPos;}
	int               getVertRange()const { return m_vRange; }
	int               getHorzRange()const { return m_hRange;}
    int               getLinesOnScreen(ScollBar type) const 
    {
        switch (type){
        case VSCROLL: return getVLinesOnScreen();
        case HSCROLL: return getHLinesOnScreen();
        }
        return 0;
    }
    int               getVLinesOnScreen() const { return (m_vClientHeight / m_vThumb) + (m_vClientHeight % m_vThumb == 0 ? 0 : 1); }
	int               getHLinesOnScreen() const { return m_hClientWidth / m_hThumb + 1; }
	int               getVertThumb() const { return m_vThumb;}
	int               getHorzThumb() const { return m_hThumb;}
    int               setThumb(ScollBar type,  int th)
    {
		ushort  thumb = (ushort)th;
        switch (type){
        case VSCROLL:  if (m_vThumb == thumb) return 0; m_vThumb = thumb; repaint(); return 1;
        case HSCROLL:  if (m_hThumb == thumb) return 0; m_hThumb = thumb; repaint(); return 1;
        }
        return 0;
    }

    void              scrollTo(ScollBar type, int pos);
	int               getScreenRow(const GPoint& point)const { return point.y / getVertThumb();}
	int               getScreenCol(const GPoint& point)const { return point.x / getHorzThumb();}
	GRect             getRowRect(int row)const { if (row < 0 || row > getVLinesOnScreen()) return GRect(); return GRect(0, row * m_vThumb, m_hClientWidth, m_vThumb); }
	GRect             getColRect(int col)const { if (col < 0 || col > getHLinesOnScreen()) return GRect(); return GRect(col * m_hThumb, 0, m_vClientHeight, m_hThumb); }
	GSize             getClientSize()const { return GSize(m_hClientWidth, m_vClientHeight);}
protected:
	virtual void      wheelEvent ( GWheelEvent& event );
	virtual void      sizeEvent( GSizeEvent& event );  //scroll with size changing!!
    virtual void      scrollEvent( GScrollEvent& event ) { scrollTo((GView::ScollBar)event.type, event.pos);}

public:
    ushort            m_vThumb, m_hThumb;
    ushort            m_vCurPos, m_hCurPos;
    ushort            m_vRange, m_hRange;
    ushort            m_hClientWidth, m_vClientHeight;
};

#endif 